<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="userPhone" label="用户手机号">
          <el-input v-model="searchData.userPhone" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="type" label="类型">
          <el-select v-model="searchData.type" placeholder="请选择" clearable style="width: 120px;">
            <el-option label="获得" :value="1" />
            <el-option label="消费" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item prop="source" label="来源">
          <el-select v-model="searchData.source" placeholder="请选择" clearable style="width: 120px;">
            <el-option label="订单" value="order" />
            <el-option label="兑换" value="exchange" />
            <el-option label="签到" value="sign" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-button type="primary" :icon="RefreshRight" @click="getPointsRecordData">刷新数据</el-button>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="pointsRecordData">
          <el-table-column type="index" width="80" align="center" label="序号" />
          <el-table-column prop="userPhone" label="用户手机号" align="center" />
          <el-table-column prop="points" label="积分变动" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.type === 1 ? 'success' : 'danger'">
                {{ scope.row.type === 1 ? '+' : '-' }}{{ scope.row.points }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="type" label="类型" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.type === 1 ? 'success' : 'warning'">
                {{ formatType(scope.row.type) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="source" label="来源" align="center">
            <template #default="scope">
              <el-tag type="info">
                {{ formatSource(scope.row.source) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="sourceId" label="来源ID" align="center" />
          <el-table-column prop="description" label="描述" align="center" show-overflow-tooltip />
          <el-table-column prop="createTime" label="创建时间" align="center" />
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { getPointsRecordListApi } from "@/api/points-record"
import { type PointsRecordData } from "@/api/points-record/types/points-record"
import { type FormInstance, ElMessage } from "element-plus"
import { Search, Refresh, RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"

defineOptions({
  // 命名当前组件
  name: "PointsRecord"
})

const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()

//#region 查
const pointsRecordData = ref<PointsRecordData[]>([])
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  userPhone: "",
  type: undefined as number | undefined,
  source: ""
})

const getPointsRecordData = () => {
  loading.value = true
  getPointsRecordListApi({
    pageNo: paginationData.currentPage,
    pageSize: paginationData.pageSize,
    userPhone: searchData.userPhone || undefined,
    type: searchData.type,
    source: searchData.source || undefined
  })
    .then(({ data }) => {
      paginationData.total = data.total
      pointsRecordData.value = data.list
    })
    .catch(() => {
      pointsRecordData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}

const handleSearch = () => {
  paginationData.currentPage === 1 ? getPointsRecordData() : (paginationData.currentPage = 1)
}

const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}

// 格式化类型
const formatType = (type: number) => {
  switch (type) {
    case 1:
      return "获得"
    case 2:
      return "消费"
    default:
      return "未知"
  }
}

// 格式化来源
const formatSource = (source: string) => {
  switch (source) {
    case "order":
      return "订单"
    case "exchange":
      return "兑换"
    case "sign":
      return "签到"
    default:
      return source
  }
}
//#endregion

/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getPointsRecordData, { immediate: true })
</script>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>